package com.supply.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.supply.pojo.LeaveBill;
import com.supply.pojo.PageData;
import com.supply.pojo.ResultBean;
import com.supply.service.LeaveBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/leaveBill")
@RestController
public class LeaveBillController {
    @Autowired
    private LeaveBillService leaveBillService;

    @GetMapping("/list")
    public ResultBean list(PageData pageData){
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<LeaveBill> leaveBills = leaveBillService.queryAll();
        return ResultBean.success(new PageInfo(leaveBills));
    }

    @GetMapping("/queryByUserId/{id}")
    public ResultBean queryByUserId(@PathVariable("id") Long id, PageData pageData){
        System.out.println("查找得到条件id：" + id);
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<LeaveBill> leaveBills = leaveBillService.queryByUserId(id);
        return ResultBean.success(new PageInfo(leaveBills));
    }

    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id){
        System.out.println("要删除的请假单id:" + id);
        int rs = leaveBillService.deleteById(id);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("服务器异常");
    }
}
